package rolesystem.middle.managers;

import java.util.HashMap;
import java.util.Map;

public class CounterManager
{
	/**
	 * The Map containing the counters (value) and their name (Key).
	 */
	private static Map <String,Integer> counterMap;
	
	/**
	 * Static constructor.
	 */
	static
	{
		counterMap=new HashMap<String,Integer>();
	}
	
	public static synchronized void add(final String counterName)
	{
		counterMap.put(counterName, 0);
	}
	
	public static synchronized void set(final String counterName, int counterValue)
	{	
		if(counterMap.containsKey(counterName))
			counterMap.put(counterName,counterValue);
	}
	
	public static synchronized int get(final String counterName)
	{
		if(counterMap.containsKey(counterName))
			return counterMap.get(counterName).intValue();
		else
			return 0;
	}
	
	public static synchronized int getAndInc(final String counterName)
	{
		if(counterMap.containsKey(counterName))
			return counterMap.put(counterName,counterMap.get(counterName).intValue()+1).intValue();
		else
			return 0;
	}
	
	public static synchronized void inc(final String counterName)
	{
		if(counterMap.containsKey(counterName))
			counterMap.put(counterName,counterMap.get(counterName).intValue()+1);
	}
	
	public static synchronized void dec(final String counterName)
	{
		if(counterMap.containsKey(counterName))
			counterMap.put(counterName,counterMap.get(counterName).intValue()-1);
	}
	
	public static synchronized void reset(final String counterName)
	{
		if(counterMap.containsKey(counterName))
			counterMap.put(counterName,0);
	}
	
	public static synchronized boolean isZero(final String counterName)
	{
		if(counterMap.containsKey(counterName) && counterMap.get(counterName).intValue()==0)
				return true;
		else 
			return false;
	}
	
	public static synchronized boolean equals(final String counterName1,final String counterName2)
	{
		if(counterMap.containsKey(counterName1) && counterMap.containsKey(counterName2) 
		   &&  counterMap.get(counterName1).intValue()==counterMap.get(counterName2).intValue())
				return true;
		else 
			return false;
	}
	
}
